﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using Acms.Bll;

public partial class UserControls_HorizontalMenu : BaseUserControl
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        menu.DataBinding += new EventHandler(menu_DataBinding);
        menu.DataBound += new EventHandler(menu_DataBound);
        if (!IsPostBack)
        {
            if (AcmsPage != null && AcmsPage.AcmsContext != null)
            {
                dsMainMenu.SiteId = AcmsPage.AcmsContext.SiteId;
                dsMainMenu.CurrentSiteVersion = AcmsPage.AcmsContext.SiteVersion;
            }
        }
    }

    void menu_DataBound(object sender, EventArgs e)
    {
        if (AcmsPage != null && AcmsPage.AcmsContext != null && AcmsPage.AcmsContext.CurrentPost != null)
        {

            var item = menu.FindItemByUrl(Page.ResolveUrl(AcmsPage.AcmsContext.CurrentPost.Url));
            if (item != null)
            {
                item.HighlightPath();
            }
        }
    }

    void menu_DataBinding(object sender, EventArgs e)
    {
        Post root = Post.GetTopLevelBySiteId(AcmsPage.AcmsContext.SiteId, AcmsPage.AcmsContext.SiteVersion);
        if (root != null)
        {
            RadMenuItem item = new RadMenuItem();
            item.Text = "Home";
            if (root != null)
            {
                item.NavigateUrl = root.Url;
            }
            else
            {
                item.NavigateUrl = "~/";
            }
            menu.Items.Add(item);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    public string CssClass
    {
        get { return menu.CssClass; }
        set { menu.CssClass = value; }
    }

    public int MaxDataBindDepth
    {
        get { return menu.MaxDataBindDepth; }
        set { menu.MaxDataBindDepth = value; }
    }

    public int Width { get; set; }

    protected override void OnPreRender(EventArgs e)
    {
        if (menu.Items.Count > 0)
        {
            int itemWidth = Width / menu.Items.Count;
            foreach (RadMenuItem i in menu.Items)
            {
                i.Width = Unit.Pixel(itemWidth);
                if (i.Items.Count > 0)
                {
                    foreach (RadMenuItem c in i.Items)
                    {
                        c.Width = Unit.Pixel(itemWidth);
                    }
                }
            }
        }
        base.OnPreRender(e);
    }
}
